package com.idanchuang.component.businesslog.es;

import com.idanchuang.component.businesslog.es.client.RestClientFactoryBean;
import com.idanchuang.component.businesslog.es.config.BusinessLogConfig;
import com.idanchuang.component.businesslog.es.config.ElasticsearchProperties;
import com.idanchuang.component.businesslog.es.config.RestClientConfigBean;
import com.idanchuang.component.businesslog.es.index.CustomRestHighLevelClient;
import com.idanchuang.component.businesslog.es.runner.InitBusinessLogRunner;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
 * <p>
 * Auto-configuration for Elasticsearch
 * </p>
 *
 * @author jiang
 * @date 2019-11-25 12:40
 */
@Configuration
@ConditionalOnClass({RestClient.class, RestClientFactoryBean.class})
public class ElasticsearchAutoConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "spring.elasticsearch.rest")
    public ElasticsearchProperties elasticsearchProperties() {
        return new ElasticsearchProperties();
    }

    @Bean
    @ConfigurationProperties(prefix = "idanchuang.component.businesslog")
    public BusinessLogConfig businessLogConfig() {
        return new BusinessLogConfig();
    }

    @Bean
    @ConfigurationProperties(prefix = "idanchuang.component.businesslog.restclient")
    public RestClientConfigBean restClientConfigBean() {
        return new RestClientConfigBean();
    }

    @Bean
    @Primary
    @ConditionalOnProperty(prefix = "idanchuang.component.businesslog", name = "indexPolicy", havingValue = "false", matchIfMissing = false)
    public RestHighLevelClient elasticsearchClient(ElasticsearchProperties properties, RestClientConfigBean restClientConfigBean) {
        RestClientFactoryBean factory = new RestClientFactoryBean();
        factory.setUris(properties.getUris());
        factory.setUsername(properties.getUsername());
        factory.setPassword(properties.getPassword());
        factory.setClientConfigBean(restClientConfigBean);
        factory.afterPropertiesSet();
        return factory.getObject();
    }

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public InitBusinessLogRunner initBusinessLogRunner(RestHighLevelClient restHighLevelClient, BusinessLogConfig businessLogConfig) {
        return new InitBusinessLogRunner(restHighLevelClient, businessLogConfig);
    }

}

